package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.ApplicationCenterBo;
import com.joker.orange.domain.vo.ApplicationCenterVo;
import com.joker.orange.domain.ApplicationCenter;
import com.joker.orange.mapper.ApplicationCenterMapper;
import com.joker.orange.service.IApplicationCenterService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 应用信息Service业务层处理
 *
 * @author Joker
 * @date 2024-06-09
 */
@RequiredArgsConstructor
@Service
public class ApplicationCenterServiceImpl implements IApplicationCenterService {

    private final ApplicationCenterMapper baseMapper;

    /**
     * 查询应用信息
     */
    @Override
    public ApplicationCenterVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询应用信息列表
     */
    @Override
    public TableDataInfo<ApplicationCenterVo> queryPageList(ApplicationCenterBo bo, PageQuery pageQuery) {
        bo.setIsDisplay("0");
        LambdaQueryWrapper<ApplicationCenter> lqw = buildQueryWrapper(bo);
        Page<ApplicationCenterVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询应用信息列表
     */
    @Override
    public List<ApplicationCenterVo> queryList(ApplicationCenterBo bo) {
        LambdaQueryWrapper<ApplicationCenter> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ApplicationCenter> buildQueryWrapper(ApplicationCenterBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ApplicationCenter> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getHyperlinkAddress()), ApplicationCenter::getHyperlinkAddress, bo.getHyperlinkAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDisplay()), ApplicationCenter::getIsDisplay, bo.getIsDisplay());
        lqw.orderByDesc(ApplicationCenter::getCreateTime);
        return lqw;
    }

    /**
     * 新增应用信息
     */
    @Override
    public Boolean insertByBo(ApplicationCenterBo bo) {
        ApplicationCenter add = BeanUtil.toBean(bo, ApplicationCenter.class);
        validEntityBeforeSave(add, bo.getApplicationLogoCopy());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改应用信息
     */
    @Override
    public Boolean updateByBo(ApplicationCenterBo bo) {
        ApplicationCenter update = BeanUtil.toBean(bo, ApplicationCenter.class);
        validEntityBeforeSave(update, bo.getApplicationLogoCopy());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ApplicationCenter entity, String applicationLogoCopy){
        entity.setApplicationLogo(applicationLogoCopy);
    }

    /**
     * 批量删除应用信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
